package com.mes.bac.backend.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mes.bac.backend.common.vo.R;
import com.mes.bac.backend.dto.PplanCreateDTO;
import com.mes.bac.backend.entity.PplanItem;
import com.mes.bac.backend.service.IPplanItemService;
import com.mes.bac.backend.service.IPplanService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Set;

@Tag(name = "4. 生产计划", description = "创建生产计划及相关操作")
@RestController
@RequestMapping("/api/pplan")
public class PplanController {

    @Autowired
    private IPplanService pplanService;

    @Autowired
    private IPplanItemService pplanItemService;

    @Operation(summary = "创建新的生产计划")
    @PostMapping("/create")
    public R<String> create(@RequestBody PplanCreateDTO pplanCreateDTO) {
        pplanService.createPplan(pplanCreateDTO);
        return R.successMessage("生产计划创建成功");
    }

    @Operation(summary = "从生产计划生成工件")
    @PostMapping("/{pplanSheetId}/generate-pieces")
    public R<String> generatePieces(@PathVariable String pplanSheetId) {
        pplanService.generatePiecesFromPlan(pplanSheetId);
        return R.successMessage("工件已成功从计划 " + pplanSheetId + " 生成");
    }

    @Operation(summary = "分页查询生产计划条目")
    @GetMapping("/page")
    public R<IPage<PplanItem>> page(
            @Parameter(description = "当前页码", example = "1") @RequestParam(defaultValue = "1") long current,
            @Parameter(description = "每页数量", example = "10") @RequestParam(defaultValue = "10") long size
    ) {
        Page<PplanItem> page = new Page<>(current, size);
        return R.success(pplanItemService.page(page));
    }

    @Operation(summary = "获取所有已生成工件的计划单号")
    @GetMapping("/generated-ids")
    public R<Set<String>> getGeneratedIds() {
        return R.success(pplanService.getGeneratedPplanSheetIds());
    }
}